package com.googlecode.slm.web.springweb;

import com.googlecode.slm.domain.web.TreeNode;
import com.googlecode.slm.domain.web.UserNode;
import com.googlecode.slm.service.TreeService;
import com.googlecode.slm.util.CONSTANTS;
import com.googlecode.slm.web.BasicControler;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpSession;

/**
 * User: liguangjian
 * Date: 11-10-7
 * Time: 下午2:47
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value="/tree")
public class TreeControler extends BasicControler{
	
	/**
	 * 不用进行认证
	 */
	public boolean isAttestation() {
		return false;
	}
    @Autowired
    @Qualifier(value = "treeService")
    private TreeService treeService;

    public TreeService getTreeService() {
        return treeService;
    }

    public void setTreeService(TreeService treeService) {
        this.treeService = treeService;
    }

    /**
     * Spring在处理@ResponseBody这个annotation的时候，org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
     * 使用了org.springframework.http.converter.StringHttpMessageConverter进行处理
     * public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
     * @see http://www.jnan.org/archives/2011/05/spring3-mvc-responsebody-annotation-encoding.html
     * @param id
     * @return
     */
//    @RequestMapping(value="/treeAjax.form")
//    @ResponseBody
//    public ResponseEntity<String> getTreeForm(int id){
//        List<TreeNode> list = getTreeService().getTreeList(id);
//        JSONArray array = new JSONArray(list, false);
//        //用于处理中文
//        HttpHeaders headers = new HttpHeaders();
//        MediaType mt=new MediaType("text","html", Charset.forName("gbk"));
//        headers.setContentType(mt);
//        ResponseEntity<String> re = new ResponseEntity<String>(array.toString() ,headers, HttpStatus.OK);
//        return re;
//    }

        /**
     * Spring在处理@ResponseBody这个annotation的时候，org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
     * 使用了org.springframework.http.converter.StringHttpMessageConverter进行处理
     * public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
         *   <mvc:annotation-driven />
     * @see http://www.jnan.org/archives/2011/05/spring3-mvc-responsebody-annotation-encoding.html
     * @param id
     * @return
     */

    @RequestMapping(value="/treeAjax.form")
    @ResponseBody
    public String getTreeForm(int pid, HttpSession session){
    	UserNode node = (UserNode) session.getAttribute(CONSTANTS.SESSION_USER_LGON);
        List<TreeNode> list = getTreeService().getUserTreeList(pid, node.getGrade());
        JSONArray array = new JSONArray(list, false);
        return array.toString();
    }
}
